package com.example.controller;

import com.example.cac409club.result.RestResult;
import com.example.constant.ResultCodeConstant;
import com.example.dto.InternalMessageDTO;
import com.example.exception.BusinessException;
import com.example.model.InternalMessageDO;
import com.example.service.InternalMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *   站内信与邮件通知控制器
 * </p>
 * @author 34448
 */
@Api(tags = "站内信与邮件通知")
@RequestMapping("/message")
@RestController
public class InternalMessageController {

    @Autowired
    private InternalMessageService internalMessageService;

    /**
     * 发送站内信
     *
     * @param internalMessageDTO 站内信信息
     * @return
     */
    @PostMapping("/send")
    @ApiOperation("发送站内信")
    public RestResult<Boolean> sendMessage(@RequestBody @Validated InternalMessageDTO internalMessageDTO) {
        try {
            boolean result = internalMessageService.sendMessage(internalMessageDTO);
            return new RestResult<>(ResultCodeConstant.CODE_000000, "调用成功", result);
        } catch (BusinessException e) {
            return new RestResult<>(e.getCode(), e.getMessage(), false);
        }
    }

    /**
     * 查询收件箱
     *
     * @param userId 用户ID
     * @return 收件箱信息列表
     */
    @GetMapping("/inbox")
    @ApiOperation("查询收件箱")
    public RestResult<List<InternalMessageDO>> getInbox(@RequestParam @NotNull Integer userId) {
        try {
            List<InternalMessageDO> result = internalMessageService.getInbox(userId);
            return new RestResult<>(ResultCodeConstant.CODE_000000, "调用成功", result);
        } catch (BusinessException e) {
            return new RestResult<>(e.getCode(), e.getMessage(), null);
        }
    }

    /**
     * 标记消息为已读
     *
     * @param messageId 消息ID
     * @return
     */
    @PutMapping("/read")
    @ApiOperation("标记消息为已读")
    public RestResult<Boolean> markAsRead(@RequestParam @NotNull Integer messageId) {
        try {
            boolean result = internalMessageService.markAsRead(messageId);
            return new RestResult<>(ResultCodeConstant.CODE_000000, "调用成功", result);
        } catch (BusinessException e) {
            return new RestResult<>(e.getCode(), e.getMessage(), false);
        }
    }

    /**
     * 删除站内信
     *
     * @param messageId 消息ID
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除站内信")
    public RestResult<Boolean> deleteMessage(@RequestParam @NotNull Integer messageId) {
        try {
            boolean result = internalMessageService.deleteMessage(messageId);
            return new RestResult<>(ResultCodeConstant.CODE_000000, "调用成功", result);
        } catch (BusinessException e) {
            return new RestResult<>(e.getCode(), e.getMessage(), false);
        }
    }

    /**
     * 发送通知邮件
     *
     * @param userId 用户ID
     * @param subject 主题
     * @param body 正文
     * @return
     */
    @PostMapping("/email/send")
    @ApiOperation("发送通知邮件")
    public RestResult<Boolean> sendNotificationEmail(
            @RequestParam @NotNull Integer userId,
            @RequestParam @NotNull String subject,
            @RequestParam @NotNull String body) {
        try {
            boolean result = internalMessageService.sendNotificationEmail(userId, subject, body);
            return new RestResult<>(ResultCodeConstant.CODE_000000, "调用成功", result);
        } catch (BusinessException e) {
            return new RestResult<>(e.getCode(), e.getMessage(), false);
        }
    }

    /**
     * 发送OTP动态邮件验证
     *
     * @param userId 用户ID
     * @return
     */
    @PostMapping("/email/otp/send")
    @ApiOperation("发送OTP动态邮件验证")
    public RestResult<Boolean> sendOTPEmail(@RequestParam @NotNull Integer userId) {
        try {
            boolean result = internalMessageService.sendOTPEmail(userId);
            return new RestResult<>(ResultCodeConstant.CODE_000000, "调用成功", result);
        } catch (BusinessException e) {
            return new RestResult<>(e.getCode(), e.getMessage(), false);
        }
    }

    /**
     * 验证OTP动态邮件验证码
     *
     * @param userId 用户ID
     * @param otpCode 验证码
     * @return
     */
    @PostMapping("/email/otp/verify")
    @ApiOperation("验证OTP动态邮件验证码")
    public RestResult<Boolean> verifyOTP(
            @RequestParam @NotNull Integer userId,
            @RequestParam @NotNull String otpCode) {
        try {
            boolean result = internalMessageService.verifyOTP(userId, otpCode);
            return new RestResult<>(ResultCodeConstant.CODE_000000, "验证码正确", result);
        } catch (BusinessException e) {
            return new RestResult<>(e.getCode(), e.getMessage(), false);
        }
    }
}